package xyz.jangle.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xyz.jangle.dao.BsUserMapper;
import xyz.jangle.model.BsUser;
import xyz.jangle.service.BsUserService;
import xyz.jangle.utils.JConstant;

@Service
public class BsUserServiceImpl implements BsUserService {
	
	@Autowired
	private BsUserMapper bsUserMapper;
	@Autowired
	private HttpSession httpSession;

	@Override
	public Map<String, Object> selectPage(BsUser bsUser) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<BsUser> list = bsUserMapper.selectPage(bsUser);
		Long count = bsUserMapper.selectPageCount(bsUser);
		map.put("list", list);
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("count", count);
		return map;
	}

	@Override
	public Map<String, Object> selectByPrimaryKey(BsUser record) {
		Map<String, Object> map = new HashMap<String, Object>();
		BsUser bsUser = bsUserMapper.selectByPrimaryKey(record.getId());
		map.put("model", bsUser);
		map.put("code", JConstant.SUCCESS_CODE);
		return map;
	}

	@Override
	public Map<String, Object> insertOrUpdate(BsUser record) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(record.getId() == null) {
			BsUser user = bsUserMapper.selectByCode(record.getCode());
			if(user == null) {
				bsUserMapper.insert(record);
			} else {
				map.put("code", "10003");
				map.put("message", "账户已存在");
				return map;
			}
		}else {
			bsUserMapper.updateByPrimaryKey(record);
		}
		map.put("model", record);
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}

	@Override
	public Map<String, Object> batchDeleteByPrimaryKey(BsUser record) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(record.getIds()!=null) {
			record.setIdsArray(record.getIds().split(","));
		}else {
			record.setIdsArray(new String[0]);
		}
		bsUserMapper.batchDeleteByPrimaryKey(record);
		map.put("model", record);
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}

	@Override
	public Map<String, Object> deleteByPrimaryKey(BsUser record) {
		Map<String, Object> map = new HashMap<String, Object>();
		bsUserMapper.deleteByPrimaryKey(record.getId());
		map.put("model", record);
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}

	@Override
	public Map<String, Object> login(String code, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		BsUser user = bsUserMapper.selectByCode(code);
		if(user == null ||!user.getPassword().equals(password)) {
			map.put("code", "10002");
			map.put("message", "账户或密码错误！");
			return map;
		}
		httpSession.setAttribute(JConstant.CODE, user.getCode());
		httpSession.setAttribute(JConstant.NAME, user.getName());
		httpSession.setAttribute(JConstant.ID, user.getId());
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}

	@Override
	public Map<String, Object> logout() {
		Map<String, Object> map = new HashMap<String, Object>();
		httpSession.removeAttribute(JConstant.CODE);
		httpSession.removeAttribute(JConstant.NAME);
		httpSession.removeAttribute(JConstant.ID);
		httpSession.invalidate();
		map.put("code", JConstant.SUCCESS_CODE);
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}

	@Override
	public Map<String, Object> status() {
		Map<String, Object> map = new HashMap<String, Object>();
		Object object = httpSession.getAttribute(JConstant.CODE);
		if(object != null) {
			map.put("code", JConstant.SUCCESS_CODE);
		}else {
			map.put("code", "10000");
		}
		map.put("message", JConstant.SUCCESS_MESSAGE);
		return map;
	}

	@Override
	public Map<String, Object> valid(String code) {
		Map<String, Object> map = new HashMap<String, Object>();
		BsUser user = bsUserMapper.selectByCode(code);
		if(user == null) {
			map.put("valid", true);
		}else {
			map.put("valid", false);
		}
		return map;
	}
	
	
	

}
